home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / language / dino / dino_bot.1 / examples / smoothing / ex3.d < prev    next >
Encoding:
Text File  |  1991-03-10  |  3.6 KB  |  121 lines

  1. /* Copyright, 1990, Regents of the University of Colorado */
  2. /* This program computes a five point smoothing algorithm (each point is
  3.  * set to be the average of his neighbors to the north, south, west, and east).
  4.  * It operates on a matrix of size M x N, blocking the matrix into small
  5.  * chuncks on a two dimensional processor array.  M must be a multiple of P1,
  6.  * and N a multiple of P2. */
  7.  
  8. #include "dino.h"
  9.  
  10. #define max(x,y) (x > y ? x : y)
  11. #define min(x,y) (x < y ? x : y)
  12.  
  13. #define M 16
  14. #define N 16
  15. #define P1 4
  16. #define P2 4
  17.  
  18. environment node[P1:id1][P2:id2] {
  19.  
  20.   composite smooth (a, in iter)
  21.   double distributed a[M][N] map FivePt;
  22.  
  23.                                 /* ==> The FivePt mapping divides the matrix a
  24.                                        into blocks of size M/P1 x N/P2.  Each
  25.                                        environment is assigned one of these
  26.                                        blocks, plus the one element wide strips
  27.                                        to each of the four sides. */
  28.  
  29.   int iter;
  30.  
  31.   {
  32.     int i, j, k;  /* Looping variables */
  33.  
  34.     int home_n, home_s, home_w, home_e;
  35.         /* Boundaries of the home data, not including the edges of
  36.          * the matrix. */
  37.  
  38.     int copy_n, copy_s, copy_w, copy_e;
  39.         /* Boundaries of the copy data, not including the edges of
  40.          * the matrix. */
  41.  
  42.                                 /* ==> To make the program easier to read and
  43.                                        understand, varaibles containing
  44.                                        the ranges of home and copy data on each
  45.                                        processor are useful. */
  46.  
  47.     /* Compute home_n, home_s, home_w, and home_e */
  48.     home_n = max (M/P1 * id1, 1);
  49.     home_s = min ((id1 + 1) * M/P1 - 1, M-2);
  50.     home_w = max (N/P2 * id2, 1);
  51.     home_e = min ((id2 + 1) * N/P2 - 1, N-2);
  52.  
  53.     /* Compute copy_n, copy_s, copy_w, and copy_e */
  54.     copy_n = max (home_n - 1, 1);
  55.     copy_s = min (home_s + 1, M-2);
  56.     copy_w = max (home_w - 1, 1);
  57.     copy_e = min (home_e + 1, N-2);
  58.  
  59.     /* Repeat the smoothing process iter times */
  60.     for (i = 0; i < iter; i++) {
  61.  
  62.       /* Send out your data and receive it back again, if not the first
  63.        * iteration */
  64.       if (i != 0) {
  65.         a[<home_n,home_s>][<home_w,home_e>]# =
  66.           a[<home_n,home_s>][<home_w,home_e>];
  67.         a[<copy_n,copy_s>][<copy_w,copy_e>]#;
  68.       }
  69.  
  70.       /* Perform the computation, but only on non-edge elements */
  71.       for (j = home_n; j <= home_s; j++)
  72.         for (k = home_w; k <= home_e; k++)
  73.           a[j][k] = (a[j][k-1] + a[j][k+1] + a[j-1][k] + a[j+1][k]) / 4;
  74.  
  75.     }
  76.   }
  77. }
  78.  
  79. environment host {
  80.  
  81.   void main ()
  82.  
  83.   {
  84.     double a[M][N];                     /* Input data */
  85.     int iter;                           /* Holds the iteration count */
  86.  
  87.     int i, j;                           /* Looping variables */
  88.  
  89.     /* Set up the initial data for a[][] */
  90.     for (i = 0; i < M; i++)
  91.       for (j = 0; j < N; j++)
  92.         a[i][j] = (i + 1)*(j + 1);
  93.     for (i = 1; i < M - 1; i++)
  94.       for (j = 1; j < N - 1; j++)
  95.         a[i][j] = 0;
  96.  
  97.     /* Set up the variable which will contain the number of iterations */
  98.     iter = 500;
  99.  
  100.     /* Print out the initial data */
  101.     printf ("Initial data for a:\n");
  102.     for (i = 0; i < M; i++) {
  103.       for (j = 0; j < N; j++)
  104.         printf ("%7.2f", a[i][j]);
  105.       printf ("\n");
  106.     }
  107.  
  108.     /* Perform the computation */
  109.     smooth (a[][], iter)#;
  110.  
  111.     /* Printout the results */
  112.     printf ("Result data for a:\n");
  113.     for (i = 0; i < M; i++) {
  114.       for (j = 0; j < N; j++)
  115.         printf ("%7.2f", a[i][j]);
  116.       printf ("\n");
  117.     }
  118.   }
  119. }
  120.  
  121.